package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.mapper.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    //推荐视频
    @GetMapping("/smallVideos")
    public PageBeanVo findRecommendVideo(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        //1. 获取登录用户的id
        Long userId = UserHolder.getUser().getId();
        return videoManager.findRecommendVideo(userId, pageNum, pageSize);
    }

    //发布视频
    @PostMapping("/smallVideos")
    public void saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        Long userId = UserHolder.getUser().getId();
        videoManager.saveVideo(userId, videoThumbnail, videoFile);
    }

    //关注用户
    @PostMapping("/smallVideos/{uid}/userFocus")
    public void userFocus(@PathVariable("uid") Long focusId) {
        videoManager.userFocus(focusId);
    }

    //取消关注用户
    @PostMapping("/smallVideos/{uid}/userUnFocus")
    public void userUnFocus(@PathVariable("uid") Long focusId) {
        videoManager.userUnFocus(focusId);
    }


  //视频点赞
    @PostMapping("/smallVideos/{id}/like")
    public Integer videoListLike(@PathVariable("id") String videoId){
        return  videoManager.videoLike(videoId);
    }

    //视频取消点赞
    @PostMapping("/smallVideos/{id}/dislike")
    public Integer videoListDisLike(@PathVariable("id") String videoId){
        return  videoManager.videoDisLike(videoId);
    }


    //查询视频评论列表
    @GetMapping("/smallVideos/{id}/comments")
    public PageBeanVo findVideoComment( @PathVariable("id") String videoId,
                                           @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize)
    {
        return videoManager.findVideoComment(videoId,pageNum,pageSize);
    }

    //对视频发表语言评论
    @PostMapping("/smallVideos/{id}/comments")
    public  Integer saveComment(@PathVariable("id") String videoId,@RequestBody Map<String, String> map){
        //1.接受参数
        String comment = map.get("comment");//评论内容

        //2.调用manager保存评论
        return videoManager.saveComment(videoId,comment);
    }


    //视频评论点赞
    @PostMapping("/smallVideos/comments/{id}/like")
    public Integer videoCommentLike(@PathVariable("id") String commentId){
        return  videoManager.videoCommentLike(commentId);
    }

    //取消视频评论点赞
    @PostMapping("/smallVideos/comments/{id}/dislike")
    public Integer videoCommentDisLike(@PathVariable("id") String commentId){
        return  videoManager.videoCommentDisLike(commentId);
    }
}
